package com.wobito.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wobito.common.constants.SysConstants;
import com.wobito.common.utils.DateUtils;
import com.wobito.mapper.CabinetLatMapper;
import com.wobito.pojo.bean.CabinetLat;
import com.wobito.pojo.dto.cabinetLat.CabinetLatDTO;
import com.wobito.pojo.dto.cabinetLat.LatAllAdminDTO;
import com.wobito.pojo.dto.cabinetLat.LatBriefDTO;
import com.wobito.pojo.dto.cabinetLat.LatByCabinetDTO;
import com.wobito.pojo.enums.IsDeletedEnum;
import com.wobito.pojo.vo.cabinetLat.AddCabinetLatVO;
import com.wobito.pojo.vo.cabinetLat.CabinetLatUpdateVO;
import com.wobito.pojo.vo.cabinetLat.UpdateCabinetLatVO;
import com.wobito.service.CabinetLatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Wangbaba
 * @Date 2024/7/1 10:14
 * @Version 1.0
 */
@Service
public class CabinetLatServiceImpl extends ServiceImpl<CabinetLatMapper, CabinetLat>
        implements CabinetLatService {

    @Autowired
    private CabinetLatMapper cabinetLatMapper;


    @Override
    public List<CabinetLatDTO> selectCabinetLatList(Long cabinetId) {
        List<CabinetLatDTO> cabinetLatDTOS=new ArrayList<>();
        List<CabinetLat> cabinetLats=cabinetLatMapper.selectList(new LambdaQueryWrapper<CabinetLat>().eq(CabinetLat::getCabinetId,cabinetId));
        if (CollectionUtil.isNotEmpty(cabinetLats)){
            cabinetLatDTOS=cabinetLats.stream().map(l->{
                CabinetLatDTO cabinetLatDTO=BeanUtil.copyProperties(l,CabinetLatDTO.class);
                return cabinetLatDTO;
            }).collect(Collectors.toList());
        }
        return cabinetLatDTOS;
    }

    @Override
    public List<LatBriefDTO> selectBrief(Long cabinetId) {
        List<LatBriefDTO> latBriefDTOS=new ArrayList<>();
        List<CabinetLat> cabinetLats=cabinetLatMapper.selectList(new LambdaQueryWrapper<CabinetLat>()
                .eq(CabinetLat::getCabinetId,cabinetId).eq(CabinetLat::getState,IsDeletedEnum.NORMAL.getCode()));
        if (CollectionUtil.isNotEmpty(cabinetLats)){
            latBriefDTOS=cabinetLats.stream().map(l->{
                LatBriefDTO latBriefDTO=BeanUtil.copyProperties(l,LatBriefDTO.class);
                return latBriefDTO;
            }).collect(Collectors.toList());
        }
        return latBriefDTOS;
    }

    @Override
    public Long selectCabinetIdBylLatId(Long latId) {
        CabinetLat cabinetLat=cabinetLatMapper.selectById(latId);
        return cabinetLat.getCabinetId();
    }

    @Override
    public List<LatByCabinetDTO> selectCabinetLats(Long cabinetId) {
        List<LatByCabinetDTO> latByCabinetDTOS = new ArrayList<>();
        List<CabinetLat> cabinetLats = cabinetLatMapper.selectList(new LambdaQueryWrapper<CabinetLat>().eq(CabinetLat::getCabinetId, cabinetId).eq(CabinetLat::getState, IsDeletedEnum.NORMAL.getCode()));
        if (CollectionUtil.isNotEmpty(cabinetLats)) {
            latByCabinetDTOS = cabinetLats.stream().map(lat -> {
                LatByCabinetDTO latByCabinetDTO = BeanUtil.copyProperties(lat, LatByCabinetDTO.class);
                return latByCabinetDTO;
            }).collect(Collectors.toList());
        }
        return latByCabinetDTOS;
    }

    @Override
    public List<LatAllAdminDTO> selectCabinetLatCodes(Long cabinetId) {
        return cabinetLatMapper.selectCabinetLatCodes(cabinetId);
    }

    @Override
    public int insertCabinetLat(AddCabinetLatVO addCabinetLatVO) {
        CabinetLat cabinetLat=BeanUtil.copyProperties(addCabinetLatVO,CabinetLat.class);
        return cabinetLatMapper.insert(cabinetLat);
    }

    @Override
    public int updateLat(UpdateCabinetLatVO updateCabinetLatVO) {
        CabinetLat cabinetLat=BeanUtil.copyProperties(updateCabinetLatVO,CabinetLat.class);
        cabinetLat.setUpdateTime(DateUtils.getNowDate());
        return cabinetLatMapper.updateById(cabinetLat);
    }

    @Override
    public int updateCabinetLat(CabinetLatUpdateVO cabinetLatUpdateVO) {
        CabinetLat cabinetLat = BeanUtil.copyProperties(cabinetLatUpdateVO, CabinetLat.class);
        cabinetLat.setUpdateTime(DateUtils.getNowDate());
        return cabinetLatMapper.update(cabinetLat, new LambdaUpdateWrapper<CabinetLat>().eq(CabinetLat::getCabinetId, cabinetLatUpdateVO.getCabinetId()).eq(CabinetLat::getCode, cabinetLatUpdateVO.getCode()));
    }

    @Override
    public int updateCabinetLatByCodes(Long cabinetId,List<Integer> codes) {
        return cabinetLatMapper.update(new CabinetLat(SysConstants.STATE_TYPE_OPEN), new LambdaUpdateWrapper<CabinetLat>().in(CabinetLat::getType, codes).eq(CabinetLat::getCabinetId,cabinetId));
    }

    @Override
    public int deleteCabinetLat() {
        return 0;
    }
}
